﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using BlueLotus7.SSQAnalysisCore;
using BlueLotus7.SSQAnalyzeWebApplication.Extensions;
using BlueLotus7.SSQAnalyzeWebApplication.ViewModel;
using BlueLotus7.SSQAnalyzeWebApplication.Properties;
using System.IO;

namespace BlueLotus7.SSQAnalyzeWebApplication.Controllers
{
    public class HistoryController : Controller
    {
        /// <summary>
        /// Index page.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            HistoryIndexInfo model = null;


            SSQLotteryDatabaseService service = new SSQLotteryDatabaseService();
            var stages = service.QuerySSQLotteryStages(null, null);
            if (stages != null && stages.Count > 0)
            {
                model = new HistoryIndexInfo();
                model.SubmitUrl = Url.Action("RetrieveHistoryInfo");
                model.LatestIdentity = stages[stages.Count - 1].LotteryIdentity;
                model.TotalStartIdentity = stages[0].LotteryIdentity;
                model.Recent100Identity = stages.Count >= 100 ? stages[stages.Count - 100].LotteryIdentity : model.TotalStartIdentity;
                model.Recent1000Identity = stages.Count >= 1000 ? stages[stages.Count - 1000].LotteryIdentity : model.TotalStartIdentity;
                model.Recent300Identity = stages.Count >= 300 ? stages[stages.Count - 300].LotteryIdentity : model.TotalStartIdentity;
                model.Recent500Identity = stages.Count >= 500 ? stages[stages.Count - 500].LotteryIdentity : model.TotalStartIdentity;
            }
            return View(model);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ActionResult HistoryInfo(DataRequest request)
        {
            if (request == null)
            {
                request = new DataRequest();
            }
            SSQLotteryDatabaseService service = new SSQLotteryDatabaseService();
            return View("HistoryInfo", service.QuerySSQMartix(request.StartStage, request.EndStage));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public PartialViewResult RetrieveHistoryInfo(DataRequest request)
        {
            if (request == null)
            {
                request = new DataRequest();
            }
            SSQLotteryDatabaseService service = new SSQLotteryDatabaseService();
            return PartialView("SSQMartixView", service.QuerySSQMartix(request.StartStage, request.EndStage));
        }

        public ActionResult Empty()
        {
            return View("HistoryInfo");
        }

        public ActionResult DownloadHistoryInfo(DataRequest request)
        {
            if (request == null)
            {
                request = new DataRequest();
            }

            string folderPath = Settings.Default.AnalysisResultFolder;
            DownloadHistoryInfoModel pageModel = new DownloadHistoryInfoModel();
            DirectoryInfo folder = new DirectoryInfo(folderPath);
            if (folder.Exists)
            {
                pageModel.StartStage = request.StartStage;
                pageModel.EndStage = request.EndStage;

                foreach (var one in folder.GetFiles("*.html"))
                {
                    //pageModel.Links.Add(new KeyValuePair<string,string>(one,Url.
                }
            }
            else
            {
                try
                {
                    SSQLotteryDatabaseService service = new SSQLotteryDatabaseService();
                    var list = service.QuerySSQMartix(request.StartStage, request.EndStage);
                    RetrieveHistoryInfoPageModel model = null;
                    int currentIndex = 0;
                    while (currentIndex < list.Count)
                    {
                        model = new RetrieveHistoryInfoPageModel();
                        var limit = currentIndex + Constants.PageSize;
                        if (limit > model.SSQMartixItems.Count)
                        {
                            limit = model.SSQMartixItems.Count;
                        }
                        for (int i = currentIndex; i < limit; i++, currentIndex++)
                        {
                            model.SSQMartixItems.Add(list[i]);
                        }
                        model.StartStage = model.SSQMartixItems[0].LotteryIdentity;
                        model.EndStage = model.SSQMartixItems[model.SSQMartixItems.Count - 1].LotteryIdentity;
                        var htmlContent = SaveHistoryAsHtml(model);
                        System.IO.File.AppendAllText(Path.Combine(folderPath, model.StartStage + "-" + model.EndStage + ".html"), htmlContent);
                    }
                }
                catch
                {
                    if (Directory.Exists(folderPath))
                    {
                        try
                        {
                            Directory.Delete(folderPath);
                        }
                        catch { }
                    }
                }
            }

            return View(pageModel);


        }

        protected string SaveHistoryAsHtml(RetrieveHistoryInfoPageModel data)
        {
            if (data != null)
            {
                return ControllerExtension.RenderRazorViewToString(this, "HistoryInfo", data);
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
